package com;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    public static final String DEFAULT_YEAR                   = "yyyy";
    public static final String DEFAULT_YEAR_MONTH             = "yyyyMM";
    public static final String DEFAULT_DATE_PATTERN           = "yyyyMMdd";
    public static final String DEFAULT_DATETIME_PATTERN       = "yyyyMMdd HH:mm:ss";
    public static final String DEFAULT_DATEHOURMIN_PATTERN    = "yyyyMMdd HH:mm";
    public static final String DEFAULT_TIMESTAMP_PATTERN      = "HH:mm:ss";
    public static final String DATETIME_DATETIMESTAMP_PATTERN = "yyyyMMddHHmmss";
    public static final String DATETIME_PATTERN               = "yyyy-MM-dd HH:mm:ss";
    public static final String TIAMDATETIME_PATTERN               = "yyyy/MM/dd HH:mm:ss";
    public static final String DEFAULT_DATE_CN           = "yyyy年MM月dd日";

    public Date nowDate() {
        return new Date(System.currentTimeMillis());
    }

    public Timestamp nowT() {
        return new Timestamp(System.currentTimeMillis());
    }

    public String date2Str(java.util.Date date) {
        return date2Str(date, DEFAULT_DATE_PATTERN);
    }
    
    public static String date2CNStr(java.util.Date date) {
        return date2Str(date, DEFAULT_DATE_CN);
    }

    public static String time2Str(java.util.Date date) {
        return date2Str(date, DEFAULT_DATETIME_PATTERN);
    }

    public static String date2Str(java.util.Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public Date str2Date(String value) {
        return str2Date(value, DEFAULT_DATE_PATTERN);
    }
    
    public static Date str2DateCn(String value) {
        return str2Date(value, DATETIME_PATTERN);
    }

    public Date str2Time(String value) {
        return str2Date(value, DEFAULT_DATETIME_PATTERN);
    }

    public Date str2HourMin(String value) {
        return str2Date(value, DEFAULT_DATEHOURMIN_PATTERN);
    }

    public static  Date str2Date(String value, String pattern) {
        if (value == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return new Date(sdf.parse(value).getTime());
        } catch (ParseException e) {
        }
        return null;
    }

    public Date addDay(java.util.Date date, int days) throws ParseException {
        return addTime(date, Calendar.DATE, days);
    }

    /**
     * 日期加减
     * 
     * @param date
     *            需要加减的日期
     * @param type
     *            类型（年--Calendar.YEAR、月--Calendar.MONTH、日--Calendar.DATE）
     * @param num
     *            加减数量（负数为减）
     * @return
     */
    public Date addTime(Date date, int type, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(type, num);
        return cal.getTime();
    }

    /**
     * 显示“刚刚”、“5分钟前”、“昨天10:23”等
     * 
     * @param date
     * @return
     */
    public String getRelTime(Date date) {
        long n = (System.currentTimeMillis() - date.getTime()) / 1000;
        String rval = date2Str(date, DEFAULT_TIMESTAMP_PATTERN);
        String val = time2Str(date);
        if (n < 60) {
            val = "刚刚";
        } else if (n < 60 * 60) {
            long $min = new Long(n / 60);
            val = $min + "分钟前";
        } else if (n < 60 * 60 * 24) {
            long $h = new Long(n / (60 * 60));
            val = $h + "小时前";
        } else if (n < 60 * 60 * 24 * 3) {
            long $d = new Long(n / (60 * 60 * 24));
            if ($d == 1)
                val = "昨天" + rval;
            else
                val = "前天" + rval;
        }
        return val;
    }

    /**
     * 显示剩余时间
     * 
     * @param date
     * @return
     */
    public String getRetainDate(Date date) {
        long n = (date.getTime() - nowDate().getTime()) / 3600 / 1000;
        String v = n + " 小时";
        long interval = (nowDate().getTime() - date.getTime()) / 1000;// 秒
        interval = Math.abs(interval);
        long day = interval / (24 * 3600);// 天
        long hour = interval % (24 * 3600) / 3600;// 小时
        long minute = interval % 3600 / 60;// 分钟
        // long second = interval % 60;// 秒
        v = hour + "小时" + minute + "分";
        if (day > 0) {
            v = day + "天" + v;
        }
        return v;
    }
    
	public static String addTimeByMinute(String startDate, int minutes) {
		SimpleDateFormat f = new SimpleDateFormat(DATETIME_PATTERN);
		Calendar c = Calendar.getInstance();
		try { 
			c.setTime(f.parse(startDate));
		} catch (Exception e) {
			e.printStackTrace();
		}  
		// add minute 
		c.add(Calendar.MINUTE, minutes);
		return f.format(c.getTime());
	}
	
	public static String addTimeBySeconds(String startDate, int seconds) {
		SimpleDateFormat f = new SimpleDateFormat(DATETIME_PATTERN);
		Calendar c = Calendar.getInstance();
		try { 
			c.setTime(f.parse(startDate));
		} catch (Exception e) {
			e.printStackTrace();
		}  
		// add SECOND 
		c.add(Calendar.SECOND, seconds);
		return f.format(c.getTime());
	}
    
    public String getTimesView(long times) {
        boolean b = times > 0;
        times = Math.abs(times) * 1000;
        if (times < 1000)
            return (b ? "-" : "") + "1秒";
        int seconds = ((Long) (times / 1000)).intValue();
        if (seconds < 60)
            return (b ? "-" : "") + seconds + "秒";
        int minutes = seconds / 60, tmp = seconds % 60;
        if (minutes < 60)
            return (b ? "-" : "") + minutes + "分" + (tmp > 0 ? (tmp + "秒") : "");
        int hours = seconds / 3600;
        minutes = (seconds % 60) / 60;
        tmp = seconds % 3600;
        return (b ? "-" : "") + hours + "小时" + (minutes > 0 ? (minutes + "分") : "") + (tmp > 0 ? (tmp + "秒") : "");
    }

    public Calendar getLastDayOfMonth(Calendar c) {
        Calendar c2 = cloneCalendar(c);
        c2.set(Calendar.DAY_OF_MONTH, 1);
        c2.roll(Calendar.DAY_OF_MONTH, -1);
        return c2;
    }

    public Calendar getLastDayOfWeek(Calendar c) {
        Calendar c2 = cloneCalendar(c);
        c2.set(Calendar.DAY_OF_WEEK, 1);
        c2.roll(Calendar.DAY_OF_WEEK, -1);
        return c2;
    }

    public Calendar getLastDayOfQuarter(Calendar c) {
        Calendar c2 = cloneCalendar(c);
        // c2.set(Calendar.DAY_OF_WEEK, 1);
        // c2.roll(Calendar.DAY_OF_WEEK, -1);
        return c2;
    }

    public Calendar cloneCalendar(Calendar c) {
        Calendar c2 = Calendar.getInstance();
        c2.setTimeInMillis(c.getTimeInMillis());
        return c2;
    }

    public String calendar2Str(Calendar c) {
        int month = c.get(Calendar.MONTH) + 1;
        String s = c.get(Calendar.YEAR) + "-" + fixDate(month) + "-" + fixDate(c.get(Calendar.DATE));
        return s;
    }

    public String fixDate(int val) {
        if (val < 10) {
            return "0" + val;
        }
        return "" + val;
    }

    /**
     * 返回当前web机器的系统日期 yyyyMMdd HH:mm:ss
     * 
     * @Title: getSystemDate
     * @param @return 设定文件
     * @return String 返回类型
     * @author sunbaojin jsun@ecomcbt.com
     * @throws
     */
    public static String getSystemDate() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }
    
    /**
     * 返回当前web机器的系统日期 yyyyMMdd HH:mm:ss
     * 
     * @Title: getSystemDate
     * @param @return 设定文件
     * @return String 返回类型
     * @author sunbaojin jsun@ecomcbt.com
     * @throws
     */
    public static String getTIAMASystemDate()  {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(TIAMDATETIME_PATTERN);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }

    /**
     * 返回当前web机器的系统日期 yyyyMMddHHmmss
     * 
     * @Title: getSystemTime
     * @param @return 设定文件
     * @return String 返回类型
     * @author wufeifei jsun@ecomcbt.com
     * @throws
     */
    public static String getSystemTime() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_DATETIMESTAMP_PATTERN);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }

    /**
     * 返回当前web机器的系统日期 yyyy-MM-dd HH:mm:ss DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"
     * 
     * @Title: getSystemTime
     * @param @return 设定文件
     * @return String 返回类型
     * @author wufeifei jsun@ecomcbt.com
     * @throws
     */
    public static String getBpmSystemTime() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }

    /**
     * 返回当前web机器的系统日期 yyyyMMdd
     * 
     * @Title: getSysDate
     * @param @return 设定文件
     * @return String 返回类型
     * @author sunbaojin jsun@ecomcbt.com
     * @throws
     */
    public static String getSysDate() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }

    /**
     * 返回当前web机器的系统年份的后两位
     * 
     * @Title: getSysDate
     * @param @return 设定文件
     * @return String 返回类型
     * @author wufeifei jsun@ecomcbt.com
     * @throws
     */
    public static String getSysYear() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_YEAR);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime()).substring(2);
        return sSysdate;
    }

    /**
     * 返回当前web机器的系统年份和日期
     * 
     * @Title: getSysYearMon
     * @param @return 设定文件
     * @return String 返回类型
     * @author wufeifei jsun@ecomcbt.com
     * @throws
     */
    public static String getSysYearMon() {
        String sSysdate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_YEAR_MONTH);
        Calendar rightNow = Calendar.getInstance();
        sSysdate = sdf.format(rightNow.getTime());
        return sSysdate;
    }

    public static int daysOfTwo(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            return -1;
        }
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(d1);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(d2);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return day2 - day1;
    }

    /**
     * 获取今天日期
     * 
     * @return
     */
    public static String today() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        return df.format(cal.getTime());
    }

    /**
     * 获取上周一日期
     * 
     * @return
     */
    public static String lastMonday() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        cal.add(Calendar.WEEK_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_WEEK, 2);

        return df.format(cal.getTime());
    }

    /**
     * 获取本周一的日期
     * 
     * @return
     */
    public static String thisMonday() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        return df.format(cal.getTime());
    }

    /**
     * 获取上周末日期
     * 
     * @return
     */
    public static String lastSunday() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        return df.format(cal.getTime());
    }

    /**
     * 获取下周一日期
     * 
     * @return
     */
    public static String nextMonday() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.WEEK_OF_YEAR, 1);

        return df.format(cal.getTime());
    }

    /**
     * 获取上个周的今天
     * 
     * @return
     */
    public static String lastWeek(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date = format.parse(strDate, new ParsePosition(0));
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - 7;

        if (day < 1) {
            month -= 1;
            if (month == 0) {
                year -= 1;
                month = 12;
            }
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                day = 30 + day;
            } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                day = 31 + day;
            } else if (month == 2) {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                    day = 29 + day;
                else
                    day = 28 + day;
            }
        }
        String y = year + "";
        String m = "";
        String d = "";
        if (month < 10)
            m = "0" + month;
        else
            m = month + "";
        if (day < 10)
            d = "0" + day;
        else
            d = day + "";

        return y + m + d;
    }

    /**
     * 获取上个月的今天
     * 
     * @return
     */
    public static String lastMonth(String strDate, int allMonth) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date = format.parse(strDate, new ParsePosition(0));
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - allMonth;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        if (month <= 0) {
            int yearFlag = (month * (-1)) / 12 + 1;
            int monthFlag = (month * (-1)) % 12;
            year -= yearFlag;
            month = monthFlag * (-1) + 12;
        } else if (day > 28) {
            if (month == 2) {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                    day = 29;
                } else
                    day = 28;
            } else if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
                day = 30;
            }
        }
        String y = year + "";
        String m = "";
        String d = "";
        if (month < 10)
            m = "0" + month;
        else
            m = month + "";
        if (day < 10)
            d = "0" + day;
        else
            d = day + "";

        return y + m + d;
    }

    /**
     * 获取两个日期的自然日差
     * 
     * @return
     */
    public static int subtractDays(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Integer.parseInt(String.valueOf(days));
    }

    /**
     * 获取两个日期之间的自然日列表
     * 
     * @param dateFirst
     *            第一个日期
     * @param dateSecond
     *            第二个日期
     * @return
     */
    public static List<String> displayDays(String dateFirst, String dateSecond) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        List<String> list = new ArrayList<>();

        try {
            Date dateOne = dateFormat.parse(dateFirst);
            Date dateTwo = dateFormat.parse(dateSecond);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            while (calendar.getTime().before(dateTwo)) {
                list.add(dateFormat.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            list.add(dateFormat.format(calendar.getTime()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 获取两个日期之间的自然月列表
     * 
     * @param dateFirst
     *            第一个日期
     * @param dateSecond
     *            第二个日期
     * @return
     */
    public static List<String> displayMonth(String dateFirst, String dateSecond) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyyMM");
        List<String> listMth = new ArrayList<>();

        try {
            Date dateOne = dateFormat.parse(dateFirst);
            Date dateTwo = dateFormat.parse(dateSecond);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            while (calendar.getTime().before(dateTwo)) {
                if (!listMth.contains(monthFormat.format(calendar.getTime()))) {
                    listMth.add(monthFormat.format(calendar.getTime()));
                }
                calendar.add(Calendar.MONTH, 1);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return listMth;
    }

    /**
     * 获取今天日期
     * 
     * @return
     */
    public static String todayt() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_WEEK, 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return df.format(cal.getTime());
    }
    


    /**
     * 获取今天日期
     * 
     * @return
     */
    public static String FindToday() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return df.format(cal.getTime());
    }

    /**
     * 获取上周一日期
     * 
     * @return
     */
    public static String lastMondayt() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        cal.add(Calendar.WEEK_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_WEEK, 2);

        return df.format(cal.getTime());
    }

    /**
     * 获取本周一的日期
     * 
     * @return
     */
    public static String thisMondayt() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        return df.format(cal.getTime());
    }

    /**
     * 获取上周末日期
     * 
     * @return
     */
    public static String lastSundayt() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        return df.format(cal.getTime());
    }

    /**
     * 获取下周一日期
     * 
     * @return
     */
    public static String nextMondayt() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.add(Calendar.WEEK_OF_YEAR, 1);

        return df.format(cal.getTime());
    }

    /**
     * 获取上个周的今天
     * 
     * @return
     */
    public static String lastWeekt(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(strDate, new ParsePosition(0));
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - 7;

        if (day < 1) {
            month -= 1;
            if (month == 0) {
                year -= 1;
                month = 12;
            }
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                day = 30 + day;
            } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                day = 31 + day;
            } else if (month == 2) {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                    day = 29 + day;
                else
                    day = 28 + day;
            }
        }
        String y = year + "";
        String m = "";
        String d = "";
        if (month < 10)
            m = "0" + month;
        else
            m = month + "";
        if (day < 10)
            d = "0" + day;
        else
            d = day + "";

        return y + "-" + m + "-" + d;
    }

    /**
     * 获取上个月的今天
     * 
     * @return
     */
    public static String lastMontht(String strDate, int allMonth) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(strDate, new ParsePosition(0));
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - allMonth;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        if (month <= 0) {
            int yearFlag = (month * (-1)) / 12 + 1;
            int monthFlag = (month * (-1)) % 12;
            year -= yearFlag;
            month = monthFlag * (-1) + 12;
        } else if (day > 28) {
            if (month == 2) {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                    day = 29;
                } else
                    day = 28;
            } else if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
                day = 30;
            }
        }
        String y = year + "";
        String m = "";
        String d = "";
        if (month < 10)
            m = "0" + month;
        else
            m = month + "";
        if (day < 10)
            d = "0" + day;
        else
            d = day + "";

        return y + "-" + m + "-" + d;
    }

    /**
     * 获取两个日期的自然日差
     * 
     * @return
     */
    public static double subtractDateToMinutes(String dateFirst, String dateSecond) {
        DateFormat df = new SimpleDateFormat(DATETIME_PATTERN);
        Date one;
        Date two;
        double minutes = 0;
        try {
            one = df.parse(dateFirst);
            two = df.parse(dateSecond);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            minutes = diff / (1000 * 60.0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return minutes;
    }

    /**
     * 获取两个日期之间的自然日列表
     * 
     * @param dateFirst
     *            第一个日期
     * @param dateSecond
     *            第二个日期
     * @return
     */
    public static List<String> displayDayst(String dateFirst, String dateSecond) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();

        try {
            Date dateOne = dateFormat.parse(dateFirst);
            Date dateTwo = dateFormat.parse(dateSecond);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            while (calendar.getTime().before(dateTwo)) {
                list.add(dateFormat.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            list.add(dateFormat.format(calendar.getTime()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 获取两个日期之间的自然月列表
     * 
     * @param dateFirst
     *            第一个日期
     * @param dateSecond
     *            第二个日期
     * @return
     */
    public static List<String> displayMontht(String dateFirst, String dateSecond) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
        List<String> listMth = new ArrayList<>();

        try {
            Date dateOne = dateFormat.parse(dateFirst);
            Date dateTwo = dateFormat.parse(dateSecond);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);

            while (calendar.getTime().before(dateTwo)) {
                if (!listMth.contains(monthFormat.format(calendar.getTime()))) {
                    listMth.add(monthFormat.format(calendar.getTime()));
                }
                calendar.add(Calendar.MONTH, 1);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return listMth;
    }

    /**
     * 处理 8 位日期 到 10 位日期
     * 
     * @param strDate
     * @return Ex.20160501 --> 2016-05-01
     */
    public static String HandleStrDate(String strDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String rstDate = "";
        try {
            Date cv = dateFormat.parse(strDate);
            rstDate = format.format(cv);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rstDate;

    }

    /**
	 * 当前日期加N个月
	 * @param string
	 * @param autonum
	 * @return
	 */
    public static String addDateByMonth(String string, String autonum, boolean isContainToday) {
		int month = Integer.parseInt(autonum);
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		Date date;
		try {
			date = dateFormat.parse(string);
			Calendar calender = Calendar.getInstance();
	        calender.setTime(date);
	        calender.add(Calendar.MONTH, month);
	        if(isContainToday)
	        	calender.add(Calendar.DATE, -1);
	        return dateFormat.format(calender.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static String addDateByMonth(String string, String autonum) {
		return addDateByMonth(string, autonum, true);
	}
	
	/**
	 * 当前日期加N个季度
	 * @param string
	 * @param autonum
	 * @return
	 */
	public static String addDateByQuter(String string, String autonum) {
		int month = Integer.parseInt(autonum);
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		Date date;
		try {
			date = dateFormat.parse(string);
			Calendar calender = Calendar.getInstance();
	        calender.setTime(date);
	        calender.add(Calendar.MONTH, month * 3);
	        calender.add(Calendar.DATE, -1);
	        return dateFormat.format(calender.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String addDateByYear(String string, String autonum) {
		int month = Integer.parseInt(autonum);
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		Date date;
		try {
			date = dateFormat.parse(string);
			Calendar calender = Calendar.getInstance();
	        calender.setTime(date);
	        calender.add(Calendar.YEAR, month);
	        calender.add(Calendar.DATE, -1);
	        return dateFormat.format(calender.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String addDateByDay(String startDate, int i) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		Date date;
		try {
			date = dateFormat.parse(startDate);
			Calendar calender = Calendar.getInstance();
	        calender.setTime(date);
	        calender.add(Calendar.DATE, i);
	        return dateFormat.format(calender.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	

	
	public static String fromatStrDate(String startDate){
		return startDate.substring(0, 4) + "-" + startDate.substring(4, 6)+ "-" + startDate.substring(6, 8);
	}
	
	public static String calcStrMon(String startDate) {
		return startDate.substring(4, 6);
	}
	
	/**
	 * 判断当前月份是否是季初月份- 1月 4月 7月 10月
	 * @param realDate
	 * @return
	 */
	public static boolean isQuterMonth(String realDate) {
		String month = realDate.substring(5,6);
		if(month.equals("1") || month.equals("4") || month.equals("7") || month.equals("10")){
			return true;
		}
		return false;
	}

	/**
	 * 判断当前日期是否是年初月份- 1月 YYYYMMDD
	 * @param realDate
	 * @return
	 */
	public static boolean isYearMonth(String realDate) {
		String month = realDate.substring(5,6);
		if(month.equals("1")){
			return true;
		}
		return false;
	}

	public static String addTMDateByHour(String tmdate, int i) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(TIAMDATETIME_PATTERN);
		Date date;
		try {
			date = dateFormat.parse(tmdate);
			Calendar calender = Calendar.getInstance();
	        calender.setTime(date);
	        calender.add(Calendar.HOUR, i);
	        return dateFormat.format(calender.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}